package br.com.JZSolution.swing;

import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

import br.com.JZSolution.modelo.Produto;
 
public class ModeloDaTabela extends AbstractTableModel {

	private static final long serialVersionUID = 1L;
	private String[] columnNames = {"Item","Código" ,"Descrição","Qtd","Un","Unit(R$)", "total item(R$)"};
    private ArrayList<Object> dataList = new ArrayList<Object>();
    private Object[][] data = new Object[0][0];
   
    public int getColumnCount() {
        return columnNames.length;
    }
    
    public void addElement(Object prod){
    	if(dataList != null){
    		
    		dataList.add(prod);
	    	
    		if(dataList.size() > 0) {
	    		configurarValores();
	    	}
    	}
    }
    
    public int getRowCount() {
        return data.length;
    }
    
    public void removerElemento(Object transf){
    	dataList.remove(transf);
    	configurarValores();
    	fireTableDataChanged();
    }
    
    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
        return data[row][col];
    }

    public Class<? extends Object> getColumnClass(int c) {
        return getValueAt(0, c)!=null?getValueAt(0, c).getClass():null;
    }

    public boolean isCellEditable(int row, int col) {
    	return false;
    }

    public void setValueAt(Object value, int row, int col) {
        data[row][col] = value;
        fireTableCellUpdated(row, col);
    }
    public void configurarValores(){
    	data = new Object[dataList.size()][columnNames.length];
    	Produto prod;
    	for (int i=0; i < dataList.size();i++) {
    		prod = (Produto)dataList.get(i);
    		data[i][0] = i;
    		data[i][1] = prod.getCodigo();
    		data[i][2] = prod.getDescricao();
    		data[i][3] = prod.getQuantidadeNaVenda();
    		data[i][4] = prod.getTipoQuantidadeVenda();
    		data[i][5] = prod.getPreco();
    		data[i][6] = prod.getTotalNaVenda();
		}
    }
    
    public ArrayList<Object> getLista(){
    	return dataList;
    }
}